<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of 爱思
 *
 * @author sunday  modify by dragon.hu @2016.2.19
 */
require_once 'BaseChannel.php';

class Aisi implements BaseChannel {

    function __construct() {
        //echo "construct";
    }

    //put your code here
    function android($data) {
    	$UID = "";
        $paymentKey = "";        
        $ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
    }

    function ios($data) {
    	$UID = "";
        $paymentKey = "";        
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
    }

	function paymentCallBack($paymentKey, $platform)
	{		
		//echo "login";
		$orderLog = new ChargeOrder();
		$orderLog->log("aisi", $_POST);
		//print_r($_POST);
		$chkres = self::chk($_POST);
		//echo " chkres = $chkres ";
		if(true == $chkres) 
		//if(result != null ) //for test 
		{ //验证通过
			 //echo " sign success ";
			$status = $_POST['status'];	//0：正常状态  1：已经兑换过
			if("0" == $status)
			{
				//进行发货
				if(false == $orderLog->recordOrder(STATUS_SUCCESS, $_POST['amount'], $_POST['billno'], "IosAiSi", $platform,$_POST['order_id']))
				{
					$ret="fail";
					$orderLog->log("aisi",array("order"=>$_POST['billno'],"money"=>$_POST['amount'],"result"=>$ret,"serverresult"=>"faild"));
					//echo " insert order:$ext error ";
				}else 
				{
					//支付成功
					$ret="success";
					$orderLog->log("aisi",array("order"=>$_POST['billno'],"money"=>$_POST['amount'],"result"=>$ret,"serverresult"=>"success"));
				}
			} else
			{	//支付失败
				//$orderNo+"支付失败！"
				//echo "  order:$ext  transaction error ";
				$orderLog->log("aisi","  order:$_POST[billno] already transaction ");
				$ret="fail";
			}
			 
		} else {
			//"$sig验证失败"
			echo "   sign error ";
			$orderLog->log("aisi","  order:$_POST[billno]  sign error ");
			$ret="fail";
		}
		return $ret;
	}

	//验证
	private function chk($notify_data)
	{
		require_once('include/aisi/Rsa.php');
		require_once('include/aisi/MyRsa.php');
		//print_r($notify_data);
		$privatedata = $notify_data['sign'];
		//error_log(date("Y-m-d h:i:s").": ".serialize($privatedata)."\r\n",3,'rsa.log');
		//echo " privatedata = $privatedata ";
		$privatebackdata = base64_decode($privatedata);
		//error_log(date("Y-m-d h:i:s")."base64_decode: ".serialize($privatebackdata)."\r\n",3,'rsa.log');
		$MyRsa = new MyRsa;
		//解密出来的数据
		$data = $MyRsa->rsa_decrypt($privatebackdata, MyRsa::public_key);
		//error_log(date("Y-m-d h:i:s")."publickey_decodeing: ".$data."\r\n",3,'rsa.log');
		//var_dump($data);
		//$rs = json_decode($data,true);
		//error_log(date("Y-m-d h:i:s")."rs ".serialize($rs)."\r\n",3,'rsa.log');
		//if(empty($rs)||empty($notify_data))return false;
		//print_r($data);
		//将解密出来的数据转换成数组
		foreach(explode('&', $data) as $val)
		{
			$arr = explode('=', $val);
			$dataArr[$arr[0]] = $arr[1];
		}
		//var_dump($dataArr);
		//error_log(date("Y-m-d h:i:s")."dataArr\t\t".var_export($dataArr, true)."\r\n",3,'rsa.log');
		//验证
		if($dataArr["billno"]==$notify_data['billno'] && $dataArr["amount"]==$notify_data['amount'] && $dataArr["status"]==$notify_data['status']) {
			return true; 
		}else{
			return false;
		}
	}
}

?>
